<?php
require("pSSO_Client.class.php");

$psk = "goG4mUrJeacE7VyidEfd";
$siteid = 1;
$ssoServer = "http://sso.yourcompany.com/";
$thisURL = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER['SCRIPT_NAME'];

session_start();

// the SSO server sent us back a token, validate it and set cookies
if (isSet($_GET['authdata']) && ($_GET['v'])) {
	$psso = new pSSO_Client($_GET['authdata'], $_GET['v'], $psk,
			$siteid, $ssoServer);

	if ($psso->authenticate()) {
		// The user is logged in, send him back to this same url
		// except without any GET params etc, so he'll be a normal
		// returning logged in user.
		header("Location: " .$thisURL);
		exit;
	} else {
		// login failed, eventhough it shouldn't have, bail out
		Throw new Exception ("Login failed:" . $psso->getError());
	}
} else {
	// We didn't get a token, either its a guest or he already has cookies
	// from a previous visit
	$psso = new pSSO_Client("", "", $psk, $siteid, $ssoServer);
	if ($psso->isLoggedIn()) {
		print("You are logged in:<br><br>");
		print("Your
				username is: " . $psso->getUserName() . "<br>");
		print("Your real name is: " . $psso->getRealName() . "<br>");
		print("Your email address is: " . $psso->getEmailAddress() . "<br>");
		print("Your timezone is: " . $psso->getTimeZone() . "<br>");
		exit;
	} else {
		print("Welcome guest, you can login <a href='" . $psso->getAuthURL($thisURL)
				. "'>here</a>");
		exit;
	}
}